home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / EGAVGA.SWG / 0048_MODE XY.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  5KB  |  178 lines

  1. {
  2. KAI ROHRBACHER
  3.  
  4. > explain MODE X.
  5.  
  6. Well,  I don't care much about Mode X (which is 320x240x256), but use Mode Y
  7. (=320x200x256)  --at least I think that this mode is called "Mode Y" (as far
  8. as  I  know, the terms were introduced by a series of Michael Abrash in "Dr.
  9. Dobb's  Journal" (?)). Nevertheless, things are identical With the exception
  10. of initialising the VGA card! So here we go; note that the Asm code examples
  11. were taken from my ANIVGA-toolkit: the PASCAL-equivalents when given are "on
  12. the  fly"  Asm->PASCAL  translations  For  improved  clarity (I hope...); in
  13. doubt, rely on the Asm part.
  14.  
  15. MODE Y in a nutshell
  16. ~~~~~~~~~~~~~~~~~~~~
  17.  
  18. Basically,  Mode  Y  works  like  this:  use  the BIOS to switch into normal
  19. 320x200x256  mode,  then reProgram the sequencer to unchain the 4 bitplanes.
  20. This  results  in  a bitplaned VRAM layout very similiar to the EGA/VGA's 16
  21. color modes:
  22. }
  23. Procedure InitGraph; Assembler;
  24. Asm
  25.   MOV AX,0013h
  26.   INT 10h
  27.   MOV DX,03C4h
  28.   MOV AL,04
  29.   OUT DX,AL
  30.   INC DX
  31.   in  AL,DX
  32.   and AL,0F7h
  33.   or  AL,04
  34.   OUT DX,AL
  35.   MOV DX,03C4h
  36.   MOV AL,02
  37.   OUT DX,AL
  38.   INC DX
  39.   MOV AL,0Fh
  40.   OUT DX,AL
  41.   MOV AX,0A000h
  42.   MOV ES,AX
  43.   SUB DI,DI
  44.   MOV AX,DI
  45.   MOV CX,8000h
  46.   CLD
  47.   REP STOSW
  48.  
  49.   MOV DX,CrtAddress
  50.   MOV AL,14h
  51.   OUT DX,AL
  52.   INC DX
  53.   in  AL,DX
  54.   and AL,0BFh
  55.   OUT DX,AL
  56.   DEC DX
  57.   MOV AL,17h
  58.   OUT DX,AL
  59.   INC DX
  60.   in  AL,DX
  61.   or  AL,40h
  62.   OUT DX,AL
  63. end;
  64.  
  65. {
  66. CrtAddress  and  StatusReg  are the port addresses For the VGA ports needed;
  67. they  are 3B4h and 3BAh on a monochrome display and 3D4h and 3DAh on a color
  68. display, but can be determined at run-time, too:
  69. }
  70.  
  71. Asm
  72.   MOV DX,3CCh
  73.   in AL,DX
  74.   TEST AL,1
  75.   MOV DX,3D4h
  76.   JNZ @L1
  77.   MOV DX,3B4h
  78.  @L1:
  79.   MOV CrtAddress,DX
  80.   ADD DX,6
  81.   MOV StatusReg,DX
  82. end;
  83.  
  84. {
  85. The  VRAM  layout  is  this:  underneath  each  memory  address in the range
  86. $A000:0000..$A000:$FFFF,  there  are  4 Bytes, each representing one pixel's
  87. color.
  88. Whenever you Write to or read from such an address, an internal logic of the
  89. VGA-card determines which one of those 4 pixels is accessed.
  90. A  line  of  320  pixels (=320 Bytes) thus only takes 320/4=80 Bytes address
  91. space,  but  to  address  a pixel, you need a) its VRAM address and b) which
  92. bitplane it's on.
  93. The  pixels  are arranged linearly: thus, the mapping from point coordinates
  94. to memory addresses is done by (x,y) <-> mem[$A000: y*80+ (x div 4)] and the
  95. bitplane is determined by (x mod 4).
  96. (Note coordinates start With 0 and that "div 4" can be computed very fast by
  97. "shr 2"; "mod 4" by "and 3").
  98.  
  99. So  you  computed the proper address and bitplane. If you want to _read_ the
  100. pixel's color, you issue commands like this:
  101.  portw[$3CE]:=(bitplane SHL 8)+4; color:=mem[$A000:y*80+(x shr 2)]
  102. Or For better speed & control, do it in Asm:
  103.  
  104.  MOV AL,4
  105.  MOV AH,bitplane
  106.  MOV DX,3CEh
  107.  CLI
  108.  OUT DX,AX
  109.  MOV AL,ES:[DI]
  110.  STI
  111.  
  112. _Writing_  a pixel's color works similiar, but needs an additional step: the
  113. mask is computed by 1 SHL bitplane (that is: 1/2/4/8 For mod4 values 0/1/2/3
  114. respectively):
  115.  portw[$3C4]:=(1 SHL bitplane+8)+2; mem[$A000:y*80+(x shr 2)]:=color
  116. Or using Asm again:
  117.  
  118.  MOV CL,bitplane
  119.  MOV AH,1
  120.  SHL AH,CL
  121.  MOV AL,2
  122.  MOV DX,3C4h
  123.  CLI
  124.  OUT DX,AX
  125.  STOSB
  126.  STI
  127.  
  128. As  stated  above, one address represents 4 pixels, so 320x200 pixels occupy
  129. 16000  address  Bytes.  We  do  have  65536  (=$A000:0..$A000:$FFFF) though,
  130. therefore  a  bit  more  than 4 pages are possible. It's up to you to define
  131. your  pages,  0..15999=page  0,  16000..31999=page  1,  32000..47999=page 2,
  132. 48000..63999=page 3, 64000..65535=unused  is the most obvious layout.
  133.  
  134. Which  part  of  the VRAM is actually displayed can be Programmed by writing
  135. the  offset  part of the starting address to the Crt-controller (the segment
  136. part is implicitly set to $A000):
  137.  
  138. Asm
  139.   MOV DX,CrtAddress
  140.   MOV AL,$0D
  141.   CLI
  142.   OUT DX,AL
  143.   INC DX
  144.   MOV AL,low Byte of starting offset
  145.   OUT DX,AL
  146.   DEC DX
  147.   MOV AL,$0C
  148.   OUT DX,AL
  149.   INC DX
  150.   MOV AL,high Byte of starting offset
  151.   OUT DX,AL
  152.   STI
  153. end;
  154.  
  155. N.B.: if you reProgram the display's starting address more often than "every
  156. now  and  then",  you  better  synchronize  that  to the vertical retrace or
  157. horizontal  enable  signal  of  your VGA card; otherwise, an annoying screen
  158. flicker will become visible during switching!
  159.  
  160. For  example,  if  you do a "FOR i:=1 to 100 do SetAddress(i*80)", this will
  161. result  in a blinding fast hardware scroll: With each iteration of the loop,
  162. the  display will start 80 address Bytes (=320 pixels = 1 row) later, giving
  163. the impression of the display scrolling upwards.
  164.  
  165. Note  that  Mode  X/Y  do  not differ in any other respect than their memory
  166. layouts  from  all  the  other  bitplaned VGA modes: palette handling is the
  167. same,  as  is usage of the VGA's Write modes! In (default) Write mode 0, you
  168. can access the VRAM by Bytes, Words or dWords. Write mode 1 is handy to copy
  169. the  contents  of  one  Graphic  page to another: you are restricted to Byte
  170. accesses, but each one will transfer 4 Bytes at once.
  171. For example, a sequence like the following...
  172. portw[$3C4]:=$0f02; portw[$3CE]:=$4105;
  173. move(mem[$a000:0000],mem[$a000:$3e80],16000);
  174. portw[$3CE]:=$4005
  175. ...enables  all 4 planes, switches to Write mode 1, copies the (64000 Bytes)
  176. contents  of  the  2nd Graphic page to the 1st one and then switches back to
  177. Write mode 0 again.
  178. }